package com.yx.xf.core.util;

import org.apache.log4j.Logger;
import org.hibernate.*;
import org.hibernate.cfg.Configuration;
 

/**
 * 
 * Helper Singleton class to manage Hibernate Sessions.
 * 
 * 
 * 
 * @author Joel Hockey
 * 
 * @version $Id: HibernateUtil.java,v 1.24 2006/08/23 18:50:50 rainerh Exp $
 */

public class HIbernateUtil {

	/** ThreadLocal Session Map */

	public static final ThreadLocal MAP = new ThreadLocal();

	private static final Logger LOG = Logger.getLogger(HIbernateUtil.class);

	private static final SessionFactory SESSION_FACTORY;

	/** Make default construct private */

	private HIbernateUtil() {
	}

	/** Loads Hibernate config. */

	static {

		try {

			LOG.debug("HibernateUtil.static - loading config");

			SESSION_FACTORY = new Configuration().configure()
					.buildSessionFactory();

			LOG.debug("HibernateUtil.static - end");

		} catch (HibernateException ex) {

			throw new RuntimeException("Exception building SessionFactory: "
					+ ex.getMessage(), ex);

		}

	}

	/**
	 * 
	 * Gets Hibernate Session for current thread. When finished, users
	 * 
	 * must return session using {@link #closeSession() closeSession()} method.
	 * 
	 * @return Hibernate Session for current thread.
	 * 
	 * @throws HibernateException
	 *             if there is an error opening a new session.
	 */

	public static Session currentSession() throws HibernateException {

		Session s = (Session) MAP.get();

		// Open a new Session, if this Thread has none yet

		if (s == null) {

			s = SESSION_FACTORY.openSession();

			MAP.set(s);

		}

		return s;

	}

	/**
	 * 
	 * Closes the Hibernate Session. Users must call this method after calling
	 * 
	 * {@link #currentSession() currentSession()}.
	 * 
	 * @throws HibernateException
	 *             if session has problem closing.
	 */

	public static void closeSession() throws HibernateException {

		Session s = (Session) MAP.get();

		MAP.set(null);

		if (s != null) {

			s.close();

		}

	}

}
